]> git.r.bdr.sh - rbdr/super-polarity/blob - Super Polarity/ParticleEngine.cs
Implements polarity system
[rbdr/super-polarity] / Super Polarity / ParticleEngine.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Graphics;
7
8 namespace SuperPolarity
9 {
10 class ParticleEngine
11 {
12 private Random random;
13 public Vector2 EmitterLocation { get; set; }
14 public Color Color;
15 private List<Particle> particles;
16 private List<Texture2D> textures;
17
18 public ParticleEngine(List<Texture2D> textures, Vector2 location)
19 {
20 EmitterLocation = location;
21 this.textures = textures;
22 this.particles = new List<Particle>();
23 random = new Random();
24 Color = Color.Red;
25 }
26
27 private Particle GenerateNewParticle()
28 {
29 Texture2D texture = textures[random.Next(textures.Count)];
30 Vector2 position = EmitterLocation;
31 Vector2 velocity = new Vector2(
32 1f * (float)(random.NextDouble() * 2 - 1),
33 1f * (float)(random.NextDouble() * 2 - 1));
34 float angle = 0;
35 float angularVelocity = 0.1f * (float)(random.NextDouble() * 2 - 1);
36 Color color = Color;
37 float size = (float)random.NextDouble();
38
39 int ttl = 20 + random.Next(40);
40
41 return new Particle(texture, position, velocity, angle, angularVelocity, color, size, ttl);
42 }
43
44 public void Update()
45 {
46 int total = 10;
47
48 for (int i = 0; i < total; i++)
49 {
50 particles.Add(GenerateNewParticle());
51 }
52
53 for (int particle = 0; particle < particles.Count; particle++)
54 {
55 particles[particle].Update();
56 if (particles[particle].TTL <= 0)
57 {
58 particles.RemoveAt(particle);
59 particle--;
60 }
61 }
62 }
63
64 public void Draw(SpriteBatch spriteBatch)
65 {
66 //spriteBatch.Begin();
67 for (int index = 0; index < particles.Count; index++)
68 {
69 particles[index].Draw(spriteBatch);
70 }
71 //spriteBatch.End();
72 }
73 }
74 }